﻿using DSkinTheme;
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace ConsoleApp2
{
    public class Program
    {

        static void Main(string[] args)
        {
            // RestClient restClient = new RestClient();
            //// restClient.EndPoint = "http://192.168.1.100:5115/chaipx";

            // string data = "/services/realtime/rainfall/getrainbyhour?date=2017-09-15&adcd=130501,130521,130522,130523,130524,130525,130526,130527,130528,130529,130530,130531,130532,130533,130534,130535,130581,130582,&stcdOrStnm=X&systemTypes=11,13,12,&column=24&sign=1&ly=21,22,";

           // string ii = restClient.doGet(data);
            //以上为RestClient类get使用实例 post可自己进源码查看 



            Console.Write("请输入端口号：");
           string port = Console.ReadLine();
            Process pro = new Process();

            // 设置命令行、参数
            pro.StartInfo.FileName = "cmd.exe";
            pro.StartInfo.UseShellExecute = false;
            pro.StartInfo.RedirectStandardInput = true;
            pro.StartInfo.RedirectStandardOutput = true;
            pro.StartInfo.RedirectStandardError = true;
            pro.StartInfo.CreateNoWindow = true;

            // 启动CMD
            pro.Start();
            // 运行端口检查命令
            pro.StandardInput.WriteLine("netstat -ano");
            pro.StandardInput.WriteLine("exit");

            // 获取结果
            Regex reg = new Regex(@"\s+", RegexOptions.Compiled);
            string line = null;
            while ((line = pro.StandardOutput.ReadLine()) != null)
            {
                line = line.Trim();
                if (line.StartsWith("TCP", StringComparison.OrdinalIgnoreCase))
                {
                    line = reg.Replace(line, ",");

                    string[] arr = line.Split(',');
                    if (arr[1].EndsWith(":" + port))
                    {
                        Console.WriteLine("8002端口的进程ID：{0}", arr[4]);
                        new Program().KillProcess(Int32.Parse(arr[4]));
                        return;
                    }

                }
            }
            Console.WriteLine("端口 {0} 未被占用", port);
            Console.ReadKey();



        }
        public void KillProcess(int processName) //调用方法，传参
        {
            try
            {
                //    //  Process[] thisproc = Process.GetProcessesByName(processName);
                Process thisproc = Process.GetProcessById(processName);
                Console.WriteLine("进程名字为：" + thisproc.ProcessName);
                if (!thisproc.CloseMainWindow()) //尝试关闭进程 释放资源
                {
                    thisproc.Kill(); //强制关闭
                }
                Console.WriteLine("进程 {0}关闭成功", processName);


            }
            catch(Exception ex) //出现异常，表明 kill 进程失败
            {
                Console.WriteLine("结束进程{ 0}出错！", processName);
            }
            finally {

                Console.ReadKey();
            }
            
        }
    }
}
